package br.ufrj.ppgi.jemf_advanced_example.medical;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Medical;
import br.ufrj.ppgi.jemf.mobile.view.MedicalView;
import br.ufrj.ppgi.jemf.utils.CustomConversions;
import br.ufrj.ppgi.jemf.utils.CustomFormat;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Medical detail screen.
 * This fragment is either contained in a {@link MedicalListActivity} in two-pane mode (on tablets) or a {@link MedicalDetailActivity} on handsets.
 */
public class MedicalDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_MEDICAL = "activated_medical";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "MEDICAL";
    /**
     * The Medical content this fragment is showing.
     */
    private static Medical medical;
    /**
     * The Medical view holder.
     */
    private static MedicalView medicalView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public MedicalDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the Medical content specified by the fragment arguments.
        if (getArguments().containsKey(MedicalDetailFragment.ARG_ID)) {
        	medical = getArguments().getParcelable(MedicalDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the Medical content
	    if (medical != null) {
	    	medicalView = new MedicalView(getActivity(), false);
	    	// Name
	    	medicalView.setMedicalNameText(medical.getName());
	    	// Gender
	    	medicalView.setMedicalGenderBool(CustomFormat.IntToBool(medical.getGender()));
	    	// Birth Date
	    	medicalView.setMedicalBirthDateText(medical.getBirthDateString());
	    	// Age
	    	medicalView.setMedicalAgeText(String.valueOf(medical.getAge()));
	    	// Login
	    	medicalView.setMedicalLoginText(medical.getLogin());
	    	// Password
	    	medicalView.setMedicalPasswordText(medical.getPassword());

	    	// Its necessary to add the MedicalView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Medical View as a child of the ScrollView.
	    	scrollView.addView(medicalView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated Medical
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_MEDICAL)) {
        	medical = savedInstanceState.getParcelable(STATE_MEDICAL);
        }
	}





	/**
     * Get updated Medical
     * @return Medical
     */
	public static Medical getMedical() {
		if (BuildConfig.DEBUG) Log.i(MedicalListActivity.TAG, "MedicalDetailFragment ::: getMedical");
    	return medical;
    }

	/**
	 * Update Medical data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((medical != null) && (medicalView != null)) {
    		if (BuildConfig.DEBUG) Log.i(MedicalListActivity.TAG, "MedicalDetailFragment ::: updateDataFromUI");
    		// Update Medical object with new data included by user through components
    		medical.setName(medicalView.getMedicalNameText());
    		medical.setGender(CustomFormat.BoolToInt(medicalView.getMedicalGenderBool()));
    		medical.setBirthDateString(medicalView.getMedicalBirthDateText());
    		if (medicalView.getMedicalAgeText().isEmpty()) {
    			medical.setAge(0);
    		} else {
    			medical.setAge(CustomConversions.birthDateToAge(medical.getBirthDate()));
    	    	// Updating Age TO UI.
    			medicalView.setMedicalAgeText(String.valueOf(medical.getAge()));
    		}
    		medical.setLogin(medicalView.getMedicalLoginText());
    		medical.setPassword(medicalView.getMedicalPasswordText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated Medical.
        outState.putParcelable(STATE_MEDICAL, medical);
    }
}
